package com.aries.ui.util;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.render.render3d.impl.AgpEngineFactory;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author: AriesHoo on 2018/7/19 9:50
 * @E-Mail: AriesHoo@126.com
 * Function: 状态栏工具类(状态栏文字颜色)
 * Description:
 * 1、修改状态栏黑白字 功能逻辑--参考 https://github.com/QMUI/QMUI_phone  QMUIStatusBarHelper类
 * 2、2019-4-11 10:42:27 新增Activity参数相关的Window参数方法
 * {@link #setStatusBarDarkMode(Window)} {@link #setStatusBarLightMode(Window)}
 * 3、2019-7-15 13:13:46 新增OPPO 4.4(含)-6.0(不含) ColorOS 状态栏黑色文字方法
 * 4、2019-8-1 10:45:32 增加设置全屏方法
 */
public class StatusBarUtil {

    public static final int STATUS_BAR_TYPE_DEFAULT = 0;
    public static final int STATUS_BAR_TYPE_MI_UI = 1;
    public static final int STATUS_BAR_TYPE_FLY_ME = 2;
    public static final int STATUS_BAR_TYPE_PHONE_M = 3;
    public static final int STATUS_BAR_TYPE_COLOR_OS = 4;

    private static final int SYSTEM_UI_FLAG_COLOR_OS_STATUS_BAR_TINT = 0x00000010;
    //系统方法无法单独获取到状态栏高度，只能获取到状态栏高度与底部导航栏高度之和，所以这里给一个固定高度
    private static final int STATUSBAR_DEFAULT_HEIGHT = 72;

    public static int setStatusBarLightMode(Ability activity) {
        if (activity == null) {
            return - 1;
        }
        return setStatusBarLightMode(activity.getWindow());
    }

    /**
     * 设置状态栏浅色模式--黑色字体图标，
     *
     * @param window window
     * @return result
     */
    public static int setStatusBarLightMode(Window window) {
        if (window == null) {
            return - 1;
        }
        int result = STATUS_BAR_TYPE_DEFAULT;

        if (setStatusBarModeForPhoneM(window, true)) {
            result = STATUS_BAR_TYPE_PHONE_M;
        }
        return result;
    }

    public static int setStatusBarDarkMode(Ability activity) {
        if (activity == null) {
            return - 1;
        }
        return setStatusBarDarkMode(activity.getWindow());
    }

    /**
     * 设置状态栏深色模式--白色字体图标，
     *
     * @param window window
     * @return result
     */
    public static int setStatusBarDarkMode(Window window) {
        int result = STATUS_BAR_TYPE_DEFAULT;

        if (setStatusBarModeForPhoneM(window, false)) {
            result = STATUS_BAR_TYPE_PHONE_M;
        }
        return result;
    }

    /**
     * 设置原生phone 6.0以上系统状态栏
     *
     * @param window window
     * @param darkText 是否把状态栏字体及图标颜色设置为深色
     * @return 统状态栏
     */
    private static boolean setStatusBarModeForPhoneM(Window window, boolean darkText) {
        if (darkText) {
            window.addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        } else {
            window.addFlags(WindowManager.LayoutConfig.MARK_LAYOUT_INSET_DECOR);
        }


        return true;
    }


    private static boolean setStatusBarModeForAndroidM2(Window window, boolean darkText) {
        boolean result = false;
        window.addFlags(WindowManager.LayoutConfig.MARK_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        Context context = window.getCurrentComponentFocus().get().getContext();
        int now = AgpEngineFactory.createEngine().get().createViewHolder(new SurfaceProvider(context)).getView().getVisibility();
//        int now = window.getCurrentComponentFocus().get().getVisibility();
        int temp = (now & WindowManager.LayoutConfig.MOD_STATUS_BAR) == WindowManager.LayoutConfig.MOD_STATUS_BAR ? now ^ WindowManager.LayoutConfig.MOD_STATUS_BAR : now;
        int systemUi = darkText ? now | WindowManager.LayoutConfig.MOD_STATUS_BAR : temp;
        window.getCurrentComponentFocus().get().setVisibility(systemUi);
        result = true;
        return result;
    }

    /**
     * 判断系统是否支持状态栏文字及图标颜色变化
     *
     * @return true支持状态栏文字颜色变化
     */
    public static boolean isSupportStatusBarFontChange() {
        return true;
    }

    /**
     * 获取状态栏高度与导航栏高度之和
     *
     * @param context context
     * @return 高度之和
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        Point point1 = new Point();
        Point point2 = new Point();
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        display.getSize(point1);
        display.getRealSize(point2);
        result = (int) ((double)point2.getPointY() - (double)point1.getPointY());

        return Math.min(result, STATUSBAR_DEFAULT_HEIGHT);
    }

    public static int getNavigationBarHeight(Context context) {
        return getStatusBarHeight(context) - STATUSBAR_DEFAULT_HEIGHT;
    }

    public static boolean isNavigationBarOpened(Context context) {
        return getStatusBarHeight(context) > STATUSBAR_DEFAULT_HEIGHT;
    }

    private static int changeStatusBarModeRetainFlag(Window window, int out) {
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        //隐藏导航栏按键
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        //隐藏导航栏背景View
        out = retainSystemUiFlag(window, out, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        return out;
    }

    public static int retainSystemUiFlag(Window window, int out, int type) {

        int now = window.getCurrentComponentFocus().get().getVisibility();
        if ((now & type) == type) {
            out |= type;
        }
        return out;
    }

    /**
     * 刘海屏控制
     *
     * @param window window 对象
     * @param fit true 控制刘海 false 退出控制
     */
    public static void fitsNotchScreen(Window window, boolean fit) {
        if (window == null) {
            return;
        }
        WindowManager.LayoutConfig lp = window.getLayoutConfig().get();
        lp.layoutInDisplaySideMode = fit ? WindowManager.LayoutConfig.LAYOUT_IN_DISPLAY_SIDE_MODE_ALWAYS : WindowManager.LayoutConfig.LAYOUT_IN_DISPLAY_SIDE_MODE_DEFAULT;
        window.setLayoutConfig(lp);
    }

    /**
     * 刘海屏控制
     *
     * @param activity Activity 对象
     * @param fit true 控制刘海 false 退出控制
     */
    public static void fitsNotchScreen(Ability activity, boolean fit) {
        fitsNotchScreen(activity.getWindow(), fit);
    }

    /**
     * 是否隐藏状态栏
     *
     * @param root 跟布局
     * @return 是否隐藏
     */
    public static boolean isStatusBar(ComponentContainer root) {
        if (root == null) {
            return false;
        }
        return (root.getVisibility() & Component.INVISIBLE) == Component.INVISIBLE;
    }

    /**
     * 隐藏状态栏
     *
     * @param window
     * @param root 跟布局
     * @param isHide 是否隐藏
     */
    public static void hideStatusBar(Window window, ComponentContainer root, boolean isHide) {
        if (root == null) {
            return;
        }
        if (isHide) {
            window.addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
            window.addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        } else {
            window.clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
            window.clearFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION);
        }
        window.addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);

//        //防止系统栏隐藏时内容区域大小发生变化
//        int uiFlags = WindowManager.LayoutConfig.MARK_ALLOW_EXTEND_LAYOUT | root.getVisibility();
//        if (isHide) {
//            if (!isStatusBar(root)) {
//                uiFlags |= WindowManager.LayoutConfig.MARK_FULL_SCREEN
//                        | Component.INVISIBLE;
//            }
//        } else {
//            if (isStatusBar(root)) {
//                uiFlags ^= Component.INVISIBLE;
//            }
//        }
//        uiFlags |= WindowManager.LayoutConfig.MARK_FULL_SCREEN;
//        root.setVisibility(uiFlags);
    }

    /**
     * 设置全屏效果
     *
     * @param window window
     * @param root 跟布局
     * @param isFull true 全屏 false 退出全屏
     */
    public static void setFullScreen(Window window, ComponentContainer root, boolean isFull) {
        hideStatusBar(window, root, isFull);
//        NavigationBarUtil.hideNavigationBar(window, isFull);
    }
}
